package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    //CREATE USER root@'%' IDENTIFIED BY '1234';  -- 创建用户并指定密码
    //GRANT ALL PRIVILEGES ON *.* TO root@'%' WITH GRANT OPTION;  --授权
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    //查询用户可以操作的菜单
    @GetMapping("/menus")
    public Result menus(){
      List<SysMenuVo> list=sysMenuService.findMenusByUserId();
        return Result.build(list, ResultCodeEnum.SUCCESS) ;
    }

    //用户分配角色 保存角色数据
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto) {
        sysUserService.doAssign(assginRoleDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    //用户删除
    @DeleteMapping(value = "/deleteById/{userId}")
    public Result deleteById(@PathVariable(value = "userId")Long userId){
        sysUserService.deleteById(userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //用户修改 todo 用户头像显示 以及修改
    @PutMapping(value = "/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //用户添加
    @PostMapping(value = "/saveSysUser")
    public  Result saveSysUser(@RequestBody SysUser sysUser){
        sysUserService.saveSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    //用户分页查询列表
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public  Result<PageInfo<SysUser>> findByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                                 @PathVariable(value = "pageSize") Integer pageSize,
                                                 SysUserDto sysUserDto){
        PageInfo<SysUser> pageInfo =sysUserService.findByPage(sysUserDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }


    //退出登录
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(name="token") String token){
        sysUserService.logOut(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //获取用户信息
    /* 1. getUserInfo(HttpServletRequest request)   request.getHeader("token");
    * 2.@RequestHeader(name = "token") String token */
//    @GetMapping(value = "/getUserInfo")
//    public  Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token){
//        SysUser  sysUser= sysUserService.getUserInfo(token);
//        return  Result.build(sysUser, ResultCodeEnum.SUCCESS);
//    }
    @GetMapping(value = "/getUserInfo")
    public  Result<SysUser> getUserInfo(){

        return  Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }


    //用户登录
    @Operation(summary = "登录方法")  //配置swagger中文方法解析
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
          LoginVo loginVo=sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS.getCode(),"操作成功");
    }

    //获取验证码
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo>generateValidateCode(){
        ValidateCodeVo validateCodeVo=validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }
}
